﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DFControlLibrary
{
    public partial class ColorBorderPanel : DoubleBufferPanel
    {
        private Color _borderColor = SystemColors.WindowFrame;

        private int _borderWidth = 3;

        private int _Radius = 10;  //円角度

        private System.Drawing.Drawing2D.GraphicsPath _path = null;

        [Description("枠色"), Category("Appearance")]
        public Color BorderColor
        {
            get
            {
                return _borderColor;
            }
            set
            {
                _borderColor = value;
            }
        }

        [Description("枠幅"), Category("Appearance")]
        public int BorderWidth
        {
            get
            {
                return _borderWidth;
            }
            set
            {
                if (_borderWidth < 0)
                    _borderWidth = 3;
                else
                    _borderWidth = value;
            }
        }

        /// <summary>圆角弧度(0为不要圆角)</summary>
        
        [Browsable(true)]
        [Description("円角度"), Category("Appearance")]
        public int RoundRadius
        {
            get
            {
                return _Radius;
            }
            set
            {
                if (value < 0)
                { _Radius = 0; }
                else
                { _Radius = value; }
                ResetRegion();
            }
        }

        public ColorBorderPanel()
        {
            InitializeComponent();
            ResetRegion();
        }

        private void ResetRegion()
        {
            if (_path == null)
            {
                _path = new System.Drawing.Drawing2D.GraphicsPath();
            }
            else
            {
                _path.Reset();
            }

            if (_Radius > 0)
            {
                float x = 0;
                float y = 0;
                float thisWidth = this.Width;
                float thisHeight = this.Height;

                //多角形の頂点の位置を設定
                // 左上角
                _path.AddArc(x, y, _Radius, _Radius, 180, 90);
                // 右上角         
                _path.AddArc(thisWidth - _Radius, y, _Radius, _Radius, 270, 90);
                // 右下角             
                _path.AddArc(thisWidth - _Radius, thisHeight - _Radius, _Radius, _Radius, 0, 90);
                // 左下角          
                _path.AddArc(x, thisHeight - _Radius, _Radius, _Radius, 90, 90);
                   
                _path.CloseAllFigures();
                
                //コントロールの形を変更
                this.Region = new Region(_path);

                // 枠線が領域より小さい
                _path.Reset();

                // 左上角
                _path.AddArc(x, y, _Radius, _Radius, 180, 90);
                // 右上角         
                _path.AddArc(thisWidth - _Radius - 1, y, _Radius, _Radius, 270, 90);
                // 右下角             
                _path.AddArc(thisWidth - _Radius - 1, thisHeight - _Radius - 1, _Radius, _Radius, 0, 90);
                // 左下角          
                _path.AddArc(x, thisHeight - _Radius - 1, _Radius, _Radius, 90, 90);

                _path.CloseAllFigures();
            }
            else
            {
                _path.AddRectangle(this.ClientRectangle);
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if (_borderWidth > 0)
            {
                Pen pen = new Pen(_borderColor, _borderWidth);
                pe.Graphics.DrawPath(pen, _path);
                //ControlPaint.DrawBorder(e.Graphics,
                //                    this.ClientRectangle,
                //                    _borderColor,
                //                    _borderWidth,
                //                    ButtonBorderStyle.Solid,
                //                    _borderColor,
                //                    _borderWidth,
                //                    ButtonBorderStyle.Solid,
                //                    _borderColor,
                //                    _borderWidth,
                //                    ButtonBorderStyle.Solid,
                //                    _borderColor,
                //                    _borderWidth,
                //                    ButtonBorderStyle.Solid);
            }
        }

        private void ColorBorderPanel_SizeChanged(object sender, EventArgs e)
        {
            ResetRegion();
        }
    }
}
